﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using IMC.Prepare.Web.Framework.Mvc;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using IMC.Prepare.Services.Blogs;
using IMC.Prepare.Data;
using IMC.Prepare.Core.Data;
using IMC.Prepare.Core.Domain.Blogs;
using IMC.Prepare.Core;
using IMC.Prepare.Web.Framework.Themes;
using IMC.Prepare.Core.Infrastructure;
using IMC.Prepare.Web.Framework.Mvc.Routes;

namespace IMC.Prepare.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            //filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            var routePublisher = EngineContext.Current.Resolve<IRoutePublisher>();
            routePublisher.RegisterRoutes(routes);

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new[] { "IMC.Prepare.Web.Controllers" }  // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            //IUnityContainer container = GetUnityContainer();
            //var serviceLocator = new UnityServiceLocator(container);
            //ServiceLocator.SetLocatorProvider(() => serviceLocator);
            //DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            //remove all view engines
            ViewEngines.Engines.Clear();
            //except the themeable razor view engine we use
            ViewEngines.Engines.Add(new ThemableRazorViewEngine());

            var dependencyResolver = new NopDependencyResolver();
            DependencyResolver.SetResolver(dependencyResolver);
        }

        //private IUnityContainer GetUnityContainer()
        //{
        //    //Create UnityContainer          
        //    IUnityContainer container = new UnityContainer()
        //    .RegisterType<IDbContext, NopObjectContext>(new HttpContextLifetimeManager<IDbContext>())
        //    .RegisterType<IPagedList<BlogPost>, PagedList<BlogPost>>(new HttpContextLifetimeManager<IPagedList<BlogPost>>())
        //    .RegisterType<IBlogService, BlogService>(new HttpContextLifetimeManager<IBlogService>())
        //    .RegisterType<IRepository<BlogPost>, EfRepository<BlogPost>>(new HttpContextLifetimeManager<IRepository<BlogPost>>())
        //      ;
        //    return container;
        //}
    }
}